class CommentController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
   unless params[:id].nil?
    link = Link.find(params[:id])    
    end
    @comments = link.comments 
    #, :per_page => 10
  end

  def show
    @comment = Comment.find(params[:id])
  end

  def new
    link = Link.find(params[:id])
    @comment = Comment.new
    link.comments << @comment
    link.save    
  end

  def create
    @comment =Comment.find(params[:comment][:id])
    @comment.comment=params[:comment][:comment]
    @comment.title=params[:comment][:title]
    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      redirect_to :action => 'list' ,:id =>params[:comment][:commentable_id]
    else
      render :action => 'new'
    end
  end

  def edit
    @comment = Comment.find(params[:id])
  end

  def update
    @comment = Comment.find(params[:id])
    if @comment.update_attributes(params[:comment])
      flash[:notice] = 'Comment was successfully updated.'
      redirect_to :action => 'show', :id => @comment
    else
      render :action => 'edit'
    end
  end

  def destroy
    Comment.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
